package com.star.service.impl;

import com.star.NotFoundException;
import com.star.dao.TypeMapper;
import com.star.pojo.Type;
import com.star.service.TypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * com.star.service
 *
 * @Description: 分类接口实现类
 * @Date: 2020-08-06 15:54
 * @Author: LENSTAR
 * @URL: http://123.57.161.114:9000/
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Transactional//实现事务操作
    @Override
    public int save(Type type) {
        return typeMapper.save(type);
    }

    @Transactional
    @Override
    public Type getType(Long id) {
        Type type = typeMapper.getType(id);
        return type;
    }

    @Override
    public Type getTypeByName(String name) {
        return typeMapper.getTypeByName(name);
    }

    @Transactional
    @Override
    public List<Type> getAllType() {
        List<Type> allType = typeMapper.getAllType();
        return allType;
    }

    @Transactional
    @Override
    public int updateType(Long id, Type type) {
        Type t = typeMapper.getType(id);
        if (t == null) {
            throw new NotFoundException("不存在该分类！");
        }
        BeanUtils.copyProperties(type, t);
        return typeMapper.updateType(t);
    }

    @Transactional
    @Override
    public int deleteType(Long id) {
        return typeMapper.deleteType(id);
    }

    @Override
    public List<Type> getAllTypeAndBlog() {
        List<Type> allTypeAndBlog = typeMapper.getAllTypeAndBlog();
        return allTypeAndBlog;
    }
}
